#include "couplecontrolview.h"
#include "ui_couplecontrolview.h"
#include "../CommunicateDelegate/communicatedelegate.h"
#include <QMessageBox>
#include "instructionEnum.h"

CoupleControlView::CoupleControlView(int styleId,CommunicateNormalDelegate *communicateNormalThreadIn,
                                     GlobalData *globalDataIn, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::CoupleControlView)
{

    communicateNormalThread=communicateNormalThreadIn;
    globalData=globalDataIn;
    ui->setupUi(this);
    setWindowFlags(Qt::FramelessWindowHint);
    setStyleSheet("background-color:white;");
    //    setGeometry(70,128,460,595);
    move(0,0);
    initialDisplayStyle(styleId);
    hide();
    model_coupleInfo = new QStandardItemModel();
    QStringList labels3 = QObject::trUtf8("变量,value,-").simplified().split(",");
    model_coupleInfo->setHorizontalHeaderLabels(labels3);
    ui->tableView_coupleControlInfo->setColumnWidth(1,120);

    ui->comboBox_axisId->clear();
    ui->comboBox_axisId->insertItem(0,"无");
    for(int i=0;i<D_ROBOT_AXIS_MAX;i++)
    {
        QString tmpStr;
        tmpStr="J";
        tmpStr+=QString::number(i+1);
        ui->comboBox_axisId->insertItem(i+1,tmpStr);
    }

}

void CoupleControlView::initialDisplayStyle(int styleId)
{
    switch (styleId) {
    case 1:
        this->setGeometry(0,0,600,450);

        ui->pushButton_return->setGeometry(500,410,90,30);
        ui->rate_GBox_3->setGeometry(10,0,260,430);
        ui->tableView_coupleControlInfo->setGeometry(10,60,240,360);
        break;
    default:
        break;
    }
}

CoupleControlView::~CoupleControlView()
{
    delete ui;
}

void CoupleControlView::showView_slot()
{
    show();
}

void CoupleControlView::on_pushButton_manualDisableAxisFollowFunction_clicked()
{
    if(ENUM_COMMAND_RUN==globalData->globalCurrentState_motion)
    {
        QMessageBox::information(this,QStringLiteral("提示"),QStringLiteral("运行状态禁止切换耦合状态"));
        return ;
    }
    QMessageBox message(QMessageBox::Question, "提示", "是否立刻解除当前轴跟随功能！"
                        , QMessageBox::Yes|QMessageBox::No , NULL);
    if(message.exec() == QMessageBox::No)
    {
        return;
    }
    int returnFlag;
    communicateNormalThread->setCoupleControlEnable(ui->comboBox_axisId->currentIndex()-1,false,returnFlag);
    if(1==returnFlag)
    {
        on_pushButton_getInfo_clicked();
        QMessageBox::information(this,QStringLiteral("提示"),QStringLiteral("解除耦合成功"));
    }
    else
    {
        QMessageBox::information(this,QStringLiteral("提示"),QStringLiteral("失败"));
    }

}

void CoupleControlView::on_pushButton_manualEnableAxisFollowFunction_clicked()
{
    if(ENUM_COMMAND_RUN==globalData->globalCurrentState_motion)
    {
        QMessageBox::information(this,QStringLiteral("提示"),QStringLiteral("运行状态禁止切换耦合状态"));
        return ;
    }
    QMessageBox message(QMessageBox::Question, "提示", "是否立刻使能当前轴跟随功能！"
                        , QMessageBox::Yes|QMessageBox::No , NULL);
    if(message.exec() == QMessageBox::No)
    {
        return;
    }
    int returnFlag;
    communicateNormalThread->setCoupleControlEnable(ui->comboBox_axisId->currentIndex()-1,true,returnFlag);
    if(1==returnFlag)
    {
        on_pushButton_getInfo_clicked();
        QMessageBox::information(this,QStringLiteral("提示"),QStringLiteral("使能耦合成功"));
    }
    else
    {
        QMessageBox::information(this,QStringLiteral("提示"),QStringLiteral("失败"));
    }
}

void CoupleControlView::on_pushButton_getInfo_clicked()
{
    std::vector<CoupleConfigParameter> infoOut;
    communicateNormalThread->getCoupleControlInfo(infoOut);

    model_coupleInfo->clear();
    //定义item
    QStandardItem* item = 0;
    int lineSize=14;

    for(int i=0;i<infoOut.size();i++)
    {
        item = new QStandardItem("主动轴");
        model_coupleInfo->setItem(lineSize*i+0,0,item);
        item = new QStandardItem(QString("%1").arg(infoOut[i].driveAxisId+1));
        model_coupleInfo->setItem(lineSize*i+0,1,item);

        item = new QStandardItem("跟随轴");
        model_coupleInfo->setItem(lineSize*i+1,0,item);
        item = new QStandardItem(QString("%1").arg(infoOut[i].followAxisId+1));
        model_coupleInfo->setItem(lineSize*i+1,1,item);

        item = new QStandardItem("耦合状态");
        model_coupleInfo->setItem(lineSize*i+2,0,item);
        if(infoOut[i].isValid)
        {
            item = new QStandardItem(QString("耦合中"));
        }
        else
        {
            item = new QStandardItem(QString("未耦合"));
        }
        model_coupleInfo->setItem(lineSize*i+2,1,item);

        item = new QStandardItem("监视距离状态");
        model_coupleInfo->setItem(lineSize*i+3,0,item);
        if(infoOut[i].isMonitorRelativeDistance)
        {
            item = new QStandardItem(QString("监视中"));
        }
        else
        {
            item = new QStandardItem(QString("不监视"));
        }
        model_coupleInfo->setItem(lineSize*i+3,1,item);

        item = new QStandardItem("当前相对距离");
        model_coupleInfo->setItem(lineSize*i+4,0,item);
        item = new QStandardItem(QString::number(infoOut[i].currentRelativeDistance,'f',3));
        model_coupleInfo->setItem(lineSize*i+4,1,item);

        item = new QStandardItem("允许负极限相对距离");
        model_coupleInfo->setItem(lineSize*i+5,0,item);
        item = new QStandardItem(QString::number(infoOut[i].allowMinRelativeDistance,'f',3));
        model_coupleInfo->setItem(lineSize*i+5,1,item);

        item = new QStandardItem("允许正极限相对距离");
        model_coupleInfo->setItem(lineSize*i+6,0,item);
        item = new QStandardItem(QString::number(infoOut[i].allowMaxRelativeDistance,'f',3));
        model_coupleInfo->setItem(lineSize*i+6,1,item);

        item = new QStandardItem("接近暂停距离");
        model_coupleInfo->setItem(lineSize*i+7,0,item);
        item = new QStandardItem(QString::number(infoOut[i].approachDistance,'f',3));
        model_coupleInfo->setItem(lineSize*i+7,1,item);

        item = new QStandardItem("耦合偏移距离");
        model_coupleInfo->setItem(lineSize*i+8,0,item);
        item = new QStandardItem(QString::number(infoOut[i].followOffsetValue,'f',3));
        model_coupleInfo->setItem(lineSize*i+8,1,item);

        item = new QStandardItem("允许跟随误差");
        model_coupleInfo->setItem(lineSize*i+9,0,item);
        item = new QStandardItem(QString("%1").arg(infoOut[i].allowMaxFollowOffset));
        model_coupleInfo->setItem(lineSize*i+9,1,item);

        item = new QStandardItem("耦合系数");
        model_coupleInfo->setItem(lineSize*i+10,0,item);
        item = new QStandardItem(QString("%1").arg(infoOut[i].coupleRatio));
        model_coupleInfo->setItem(lineSize*i+10,1,item);

        item = new QStandardItem("耦合类型");
        model_coupleInfo->setItem(lineSize*i+11,0,item);
        if(E_COUPLE_TYPE_COMMON==infoOut[i].coupleType)
        {
            item = new QStandardItem(QString("普通"));
        }
        else
        {
            item = new QStandardItem(QString("地图"));
        }
        model_coupleInfo->setItem(lineSize*i+11,1,item);


    }


    ui->tableView_coupleControlInfo->setModel(model_coupleInfo);
    ui->tableView_coupleControlInfo->resizeColumnToContents(0);
    ui->tableView_coupleControlInfo->resizeColumnToContents(1);
    ui->tableView_coupleControlInfo->resizeColumnToContents(2);
    ui->tableView_coupleControlInfo->show();
}

void CoupleControlView::on_pushButton_return_clicked()
{
    hide();
}

void CoupleControlView::on_pushButton_manualDisableMonitorDistance_clicked()
{

    QMessageBox message(QMessageBox::Question, "提示", "是否立刻解除当前轴距离监视！"
                        , QMessageBox::Yes|QMessageBox::No , NULL);
    if(message.exec() == QMessageBox::No)
    {
        return;
    }
    int returnFlag;
    communicateNormalThread->setCoupleRelativeDistanceMonitorEnable(ui->comboBox_axisId->currentIndex()-1,false,returnFlag);
    if(1==returnFlag)
    {
        on_pushButton_getInfo_clicked();
        QMessageBox::information(this,QStringLiteral("提示"),QStringLiteral("解除距离监视成功"));
    }
    else
    {
        QMessageBox::information(this,QStringLiteral("提示"),QStringLiteral("失败"));
    }

}

void CoupleControlView::on_pushButton_manualEnableMonitorDistance_clicked()
{

    QMessageBox message(QMessageBox::Question, "提示", "是否立刻使能当前轴距离监视！"
                        , QMessageBox::Yes|QMessageBox::No , NULL);
    if(message.exec() == QMessageBox::No)
    {
        return;
    }
    int returnFlag;
    communicateNormalThread->setCoupleRelativeDistanceMonitorEnable(ui->comboBox_axisId->currentIndex()-1,true,returnFlag);
    if(1==returnFlag)
    {
        on_pushButton_getInfo_clicked();
        QMessageBox::information(this,QStringLiteral("提示"),QStringLiteral("使能距离监视成功"));
    }
    else
    {
        QMessageBox::information(this,QStringLiteral("提示"),QStringLiteral("失败"));
    }
}
